/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.indexer.document.constructor;

import java.sql.SQLException;
import java.util.List;

import org.jftstool.config.DBAttributeConfig;
import org.jftstool.enums.DataTypes;
import org.jftstool.indexer.document.DBDocument;
import org.jftstool.indexer.document.DBField;
import org.jftstool.indexer.document.exceptions.DBDocumentExtractorException;


/**
 * Class responsible for removing documents from a database. 
 * @author grupo JFTS
 */
public class DefaultDBDocumentExtractor extends DBDocumentsExtractor {

	public DefaultDBDocumentExtractor() throws DBDocumentExtractorException {
		super();
	}

	@Override
	public DBDocument extractData(final List<DBAttributeConfig> attributes) throws DBDocumentExtractorException {
		String fieldType = null;
		String fieldName = null;
		String dbAttribute = null;
		
		DBDocument doc = new DBDocument();
		try{
			for(DBAttributeConfig att : attributes){
				if (att.getArg("type").equalsIgnoreCase(DataTypes.CLOB.toString()) || att.getArg("type").equalsIgnoreCase(DataTypes.BLOB.toString())){
					byte [] buffer = rs.getBytes(att.getArg("name"));
					doc.setDocument(buffer);
					doc.setDocumentField(att.getArg("field"));
				}
				else{
					fieldType = att.getArg("type");
					fieldName = att.getArg("field");
					dbAttribute = att.getArg("name");
					
					if (fieldType.equalsIgnoreCase(DataTypes.BOOLEAN.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getBoolean(dbAttribute), DataTypes.BOOLEAN));
					}
					else if (fieldType.equalsIgnoreCase(DataTypes.BYTE.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getByte(dbAttribute), DataTypes.BYTE));						
					}
					else if (fieldType.equalsIgnoreCase(DataTypes.DATE.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getDate(dbAttribute), DataTypes.DATE));						
					}					
					else if (fieldType.equalsIgnoreCase(DataTypes.DOUBLE.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getDouble(dbAttribute), DataTypes.DOUBLE));						
					}					
					else if (fieldType.equalsIgnoreCase(DataTypes.FLOAT.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getFloat(dbAttribute), DataTypes.FLOAT));						
					}					
					else if (fieldType.equalsIgnoreCase(DataTypes.INT.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getInt(dbAttribute), DataTypes.INT));						
					}					
					else if (fieldType.equalsIgnoreCase(DataTypes.LONG.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getLong(dbAttribute), DataTypes.LONG));						
					}					
					else if (fieldType.equalsIgnoreCase(DataTypes.SHORT.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getShort(dbAttribute), DataTypes.SHORT));						
					}					
					else if (fieldType.equalsIgnoreCase(DataTypes.STRING.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getString(dbAttribute), DataTypes.STRING));
					}					
					else if (fieldType.equalsIgnoreCase(DataTypes.TIME.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getTime(dbAttribute), DataTypes.TIME));						
					}					
					else if (fieldType.equalsIgnoreCase(DataTypes.TIMESTAMP.toString())){
						doc.setField(fieldName, new DBField(fieldName, rs.getTimestamp(dbAttribute), DataTypes.TIMESTAMP));						
					}
					else {
						throw new DBDocumentExtractorException("Unsupported type for attribute: " + att.getArg("name"));
					}
				}
			}
		}
		catch(SQLException e){
			throw new DBDocumentExtractorException("Error during db extract data");
		}
		return doc;
	}

}
